package cn.forkway.learn.jdbcTry;

import javax.xml.stream.FactoryConfigurationError;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;


/**
 * @program: java-learn-jdbc
 * @className: demo02.java
 * @author: River Ray
 * @description: 用户登录第一版 statement实现，可能会被注入
 * @createTime 2021年10月08日 15:30:00
 */
public class demo02 {
    public static void main(String[] args) {
        String out = "";
        //recycle
        while (!out.equals("success")){
            Map<String, String> userLoginInfo = initUI();
            Boolean success = login(userLoginInfo);
            out = success ? "success" : "fail";
            System.out.println(out);
        }

    }

    /**
     * 用户登录
     *
     * @param userLoginInfo
     * @return false 失败 ;true  成功；
     */
    private static Boolean login(Map<String, String> userLoginInfo) {

        boolean loginSuccess = false;

        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String URL = "jdbc:mysql://localhost:3306/jdbctest?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=Asia/Shanghai";
            String USER = "root";
            String PWD = "250032";
            conn = DriverManager.getConnection(URL, USER, PWD);
            stmt = conn.createStatement();
            String sql = String.format("select * from t_user where loginName='%s' and loginPwd ='%s';",userLoginInfo.get("LoginName"),userLoginInfo.get("pwd"));
            System.out.println(sql);
            rs = stmt.executeQuery(sql);

            if (rs.next()) {
                loginSuccess = true;
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }

        return loginSuccess;
    }

    private static Map<String, String> initUI() {

        Scanner s = new Scanner(System.in);
        System.out.print("userName-->");
        String loginName = s.nextLine();
        System.out.print("pwd-->");
        String pwd = s.nextLine();
//      SQL注入
//      sdasdsa' or "1"='1
        Map<String, String> userLoginInfo;
        userLoginInfo = new HashMap<>();
        userLoginInfo.put("LoginName", loginName);
        userLoginInfo.put("pwd", pwd);
        return userLoginInfo;
    }
}


